package com.han.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * 文件目录工具类
 * 
 * @author guqc_ebiz 2016-05-30
 * @version 1.0
 */
public class PathUtil {

	/**
	 * 从文件夹、压缩包找到指定格式的文件列表
	 * 
	 * @param sourceFilePath
	 *            文件夹、压缩包
	 * @param destFormat
	 *            目标文件格式
	 * @return 文件列表
	 */
	public static List<File> fetchFile(String sourceFilePath, String destFormat) {
		try {
			File sourceFile = new File(sourceFilePath);
			if (sourceFilePath.toLowerCase().endsWith("." + destFormat)) {
				return Collections.singletonList(sourceFile);
			}
			List<File> fileList = new ArrayList<File>();

			if (sourceFilePath.toLowerCase().endsWith(".zip")
					|| sourceFilePath.toLowerCase().endsWith(".rar")) {
				String fileName = getFileName(sourceFilePath);
				String dirPath = getDirPath(sourceFilePath);
				String subDir = fileName.substring(0, fileName.length() - 4);
				String tempPath = joinPath(dirPath, subDir);
				ZipUtil.deCompress(sourceFilePath, tempPath);
				fileList.addAll(fetchFile(tempPath, destFormat));
			} else if (sourceFile.isDirectory()) {
				File[] subFileA = sourceFile.listFiles();
				if (subFileA != null && subFileA.length > 0) {
					for (File subFile : subFileA) {
						if (subFile.getName().toLowerCase()
								.endsWith("." + destFormat)) {
							fileList.add(subFile);
						} else {
							fileList.addAll(fetchFile(subFile.getPath(),
									destFormat));
						}

					}
				}
			}
			return fileList;
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		}
	}

	/**
	 * 连接目录,返回目录路径以目录分割符结束
	 * 
	 * @param outerDir
	 *            外层目录
	 * @param innerDirs
	 *            内层目录
	 * @return
	 */
	public static String joinDirPath(final String outerDir, String... innerDirs) {
		String pathSpliter = "/";
		if (outerDir.indexOf('\\') >= 0) {
			pathSpliter = "\\";
		}
		String fullPath = outerDir.trim();
		if (!fullPath.endsWith(pathSpliter)) {
			fullPath += pathSpliter;
		}
		for (int i = 0; i < innerDirs.length; i++) {
			String innerDirOrFile = innerDirs[i];
			if (innerDirOrFile == null || innerDirOrFile.trim().length() == 0) {
				continue;
			}
			if (innerDirOrFile.indexOf('\\') >= 0 && pathSpliter.equals("/")) {
				innerDirOrFile = StringUtil.replace(innerDirOrFile, "\\",
						pathSpliter);
			}

			if (innerDirOrFile.indexOf('/') >= 0 && pathSpliter.equals("\\")) {
				innerDirOrFile = StringUtil.replace(innerDirOrFile, "/",
						pathSpliter);
			}

			if (!fullPath.endsWith(pathSpliter)
					&& !innerDirOrFile.startsWith(pathSpliter)) {
				fullPath += pathSpliter;
			} else if (fullPath.endsWith(pathSpliter)
					&& innerDirOrFile.startsWith(pathSpliter)) {
				fullPath = fullPath.substring(0, fullPath.length() - 1);
			}

			fullPath += innerDirOrFile;
		}
		if (!fullPath.endsWith(pathSpliter)) {
			fullPath += pathSpliter;
		}
		return fullPath;
	}

	/**
	 * 连接文件路径
	 * 
	 * @param outerDir
	 *            外层目录
	 * @param innerDirOrFiles
	 *            内层目录或文件路径
	 * @return
	 */
	public static String joinPath(final String outerDir,
			String... innerDirOrFiles) {
		String pathSpliter = "/";
		if (outerDir.indexOf('\\') >= 0) {
			pathSpliter = "\\";
		}
		String fullPath = outerDir.trim();
		if (!fullPath.endsWith(pathSpliter)) {
			fullPath += pathSpliter;
		}
		for (int i = 0; i < innerDirOrFiles.length; i++) {
			String innerDirOrFile = innerDirOrFiles[i];
			if (innerDirOrFile == null || innerDirOrFile.trim().length() == 0) {
				continue;
			}
			if (innerDirOrFile.indexOf('\\') >= 0 && pathSpliter.equals("/")) {
				innerDirOrFile = StringUtil.replace(innerDirOrFile, "\\",
						pathSpliter);
			}

			if (innerDirOrFile.indexOf('/') >= 0 && pathSpliter.equals("\\")) {
				innerDirOrFile = StringUtil.replace(innerDirOrFile, "/",
						pathSpliter);
			}

			if (!fullPath.endsWith(pathSpliter)
					&& !innerDirOrFile.startsWith(pathSpliter)) {
				fullPath += pathSpliter;
			} else if (fullPath.endsWith(pathSpliter)
					&& innerDirOrFile.startsWith(pathSpliter)) {
				fullPath = fullPath.substring(0, fullPath.length() - 1);
			}

			fullPath += innerDirOrFile;
		}
		return fullPath;
	}

	/**
	 * 从文件全路径中获取文件所在目录
	 * 
	 * @param filePath
	 *            文件全路径
	 * @return
	 */
	public static String getDirPath(String filePath) {
		String pathSpliter = "/";
		if (filePath.indexOf('\\') >= 0) {
			pathSpliter = "\\";
		}

		String dirPath = filePath.substring(0,
				filePath.lastIndexOf(pathSpliter) + 1);

		return dirPath;
	}

	/**
	 * 从文件路径中获取文件名
	 * 
	 * @param filePath
	 *            文件全路径
	 * @return
	 */
	public static String getFileName(String filePath) {
		String pathSpliter = "/";
		if (filePath.indexOf('\\') >= 0) {
			pathSpliter = "\\";
		}
		String fileName = filePath
				.substring(filePath.lastIndexOf(pathSpliter) + 1);
		return fileName;
	}

	/**
	 * 得到相对路径
	 * 
	 * @param baseDir
	 *            基础目录
	 * @param fullPath
	 *            绝对路径
	 * @return
	 */
	public static String getRelativePath(final String baseDir,
			final String fullPath) {
		String pathSpliter = "/";
		if (baseDir.indexOf('\\') >= 0) {
			pathSpliter = "\\";
		}

		String cortFullPath = fullPath;
		String cortBaseDir = baseDir;
		if (!cortBaseDir.endsWith(pathSpliter)) {
			cortBaseDir += pathSpliter;
		}
		if (cortFullPath.indexOf('\\') >= 0 && pathSpliter.equals("/")) {
			cortFullPath = StringUtil.replace(cortFullPath, "\\", pathSpliter);
		}

		if (cortFullPath.indexOf('/') >= 0 && pathSpliter.equals("\\")) {
			cortFullPath = StringUtil.replace(cortFullPath, "/", pathSpliter);
		}

		if (cortFullPath.length() < cortBaseDir.length()) {
			if ((fullPath + pathSpliter).equals(cortBaseDir)) {
				return "";
			} else {
				throw new RuntimeException(fullPath + " is not in " + baseDir);
			}
		}

		if (!cortFullPath.startsWith(cortBaseDir)) {
			throw new RuntimeException(fullPath + " is not in " + baseDir);
		}

		return cortFullPath.substring(cortBaseDir.length());
	}

}
